from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.response import Response
from rest_framework.views import APIView
from datetime import datetime
import time

from six_it.models import Gathering


class Registrationactivities(APIView):
    permission_classes = [IsAuthenticated]
    def post(self, request, id):
        user = request.user
        gathering = Gathering.objects.get(id=id)
        print(gathering.endrolltime, type(gathering.endrolltime))

        end = datetime.strptime(str(gathering.endrolltime)[0:9], '%Y-%m-%d')
        today  = datetime.strptime(str(datetime.now())[0:9], '%Y-%m-%d')


        if today > end:
            raise ValueError('结束了')


        if user in gathering.users.all():
            gathering.users.remove(user)
            return Response({
                "message": "取消报名",
                "success": True
            })
        else:
            gathering.users.add(user)
            gathering.save()
            return Response({
                "message": "报名成功",
                "success": True
            })
